package com.yiaohuer.service.impl;

import com.yiaohuer.DTO.CampusDTO;
import com.yiaohuer.VO.CampusVO;
import com.yiaohuer.convert.CampusConverter;
import com.yiaohuer.dao.CampusDao;
import com.yiaohuer.entity.Campus;
import com.yiaohuer.entity.PageResult;
import com.yiaohuer.enums.IsDeletedFlagEnum;
import com.yiaohuer.service.CampusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 秋招数据表;(Campus)表服务实现类
 *
 * @author makejava
 * @since 2024-09-27 21:06:48
 */
@Service("campusService")
@Slf4j
public class CampusServiceImpl implements CampusService {

    @Resource
    private CampusDao campusDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Campus queryById(Integer id) {
        return this.campusDao.queryById(id);
    }


    /**
     * 新增数据
     *

     * @return 实例对象
     */
    @Override
    public Campus insert(CampusDTO campusDTO) {
        Campus campus = new Campus();
        Date now = new Date();
        BeanUtils.copyProperties(campusDTO,campus);

        campus.setCreateBy("yiaohuer");
        campus.setCreatTime(now);
        campus.setUpdateTime(now);
        campus.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        this.campusDao.insert(campus);
        return campus;
    }

    /**
     * 修改数据
     *
     * @param campusDTO 实例对象
     * @return 实例对象
     */
    @Override
    public Campus update(CampusDTO campusDTO) {
        Campus campus = new Campus();
        Date now = new Date();
        BeanUtils.copyProperties(campusDTO,campus);
        campus.setUpdateTime(now);

        this.campusDao.update(campus);
        return this.queryById(campus.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        Campus campus = new Campus();

        campus.setId(id);
        campus.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        Date now = new Date();
        campus.setUpdateTime(now);

        return this.campusDao.update(campus) > 0;
    }

    @Override
    public List<Campus> queryAllCampus(CampusDTO campusDTO) {
        return this.campusDao.queryAllByLimit(campusDTO);
    }

    @Override
    public PageResult<Campus> getCampusPage(CampusDTO campusDTO) {
        log.info("com.yiaohuer.campus.service.getCampusPage.campusDTO:{}",campusDTO);

        PageResult<Campus> pageResult = new PageResult<>();
        pageResult.setPageNo(campusDTO.getPageNo());
        pageResult.setPageSize(campusDTO.getPageSize());

        int start = (campusDTO.getPageNo() - 1) * campusDTO.getPageSize();
        Campus campus = new Campus();

        BeanUtils.copyProperties(campusDTO,campus);

        int count = campusDao.countByCondition(campus);
        System.out.println(count);
        if (count == 0) {
            return pageResult;
        }

        log.info("com.yiaohuer.campus.service.getCampusPage.campus:{}",campus);

        List<Campus> campusList = campusDao.queryPage(campus, start, campusDTO.getPageSize());
        log.info("com.yiaohuer.campus.service.queryPage.campus:{}",campusList);

        //List<CampusVO> campusVOList = CampusConverter.INSTANCE.convertListToVO(campusList);
        pageResult.setRecords(campusList);
        pageResult.setTotal(count);
        return pageResult;
    }
}
